//
//  YSZNewFeatureCollectionViewController.swift
//  swiftweibo6
//
//  Created by ysz on 15/11/26.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit



class YSZNewFeatureCollectionViewController: UICollectionViewController {

    //流水布局
    let layout = UICollectionViewFlowLayout()
    //属性
    private let ReuseIdentifier = "ReuseIdentifier"
    private let ImageCount=4
    
    init()
    {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
    }

    private func prepareCollectionView()
    {
        //collectionView注册cell
        collectionView?.registerClass(YSZNewFeatureCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        //设置布局
        //item 大小
        layout.itemSize = view.bounds.size
        //滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置距离
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
        //分页 
        collectionView?.pagingEnabled=true
        //取消弹簧
        collectionView?.bounces=false
        //取消下面的滚动条
        collectionView?.showsHorizontalScrollIndicator=false
        
    }

    // CollectionView要显示数据需要实现数据源方法
    /// CollectionView显示cell的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return ImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! YSZNewFeatureCell
        cell.index = indexPath.item
        
    
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        
        if visibleIndexPath.item == ImageCount - 1
        {
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! YSZNewFeatureCell
            cell.startAnimation()
        }
        
    }
}

class YSZNewFeatureCell: UICollectionViewCell
{
    
    var index: Int = 0
    {
        didSet{
            backgroundImageView.image = UIImage(named: "new_feature_\(index + 1)")
            //按钮
            startButton.hidden=true
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepare()
    }
    //准备ui
    private func prepare()
    {
        //添加子控件
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(startButton)
        backgroundImageView.backgroundColor = UIColor.orangeColor()
        //添加约束
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints=false
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg":backgroundImageView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg":backgroundImageView]))
        // 开始按钮
        startButton.translatesAutoresizingMaskIntoConstraints=false
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    func buttonClick()
    {
        AppDelegate.switchRootViewController(true)
    }
    
    func startAnimation()
    {
        startButton.hidden=false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 设置X/Y缩放都为1, 清空到原始状态(X/Y缩放都为1)
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("动画完成")
        }

    }
    
    
    //懒加载控件
    private lazy var backgroundImageView = UIImageView()
    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()

    
    
    
}
